package com.woniuxy.woniuhis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.woniuhis.controller.exception.NullCartException;
import com.woniuxy.woniuhis.controller.exception.NullPharmacyException;
import com.woniuxy.woniuhis.entity.Emp;
import com.woniuxy.woniuhis.entity.OrderCart;
import com.woniuxy.woniuhis.entity.OrderList;
import com.woniuxy.woniuhis.entity.Pharmacy;
import com.woniuxy.woniuhis.exception.MedicineServiceException;
import com.woniuxy.woniuhis.service.MedicineCostService;
import com.woniuxy.woniuhis.service.OrderCartService;
import com.woniuxy.woniuhis.service.OrderService;
import com.woniuxy.woniuhis.service.PharmacyService;
import com.woniuxy.woniuhis.util.ResponseInfo;


@Controller
public class OrderListController {

    @Autowired
    OrderCartService orderCartService;
    
    @Autowired
    MedicineCostService medicineCostService;
    
    @Autowired
    PharmacyService pharmacyService;
    
    @Autowired
    OrderService orderService;
    

    
    /**
     * @param req
     * @return
     * 创建订单增加方法
     */
    
    @RequestMapping("getList")
    @ResponseBody
    public ResponseInfo getListController(HttpServletRequest req) {
    	
		
    	try {
    		
    		List<OrderList> allList = orderService.findAllOrderList();
    		
    		
    		ResponseInfo responseInfo = new ResponseInfo();
    		responseInfo.setData(allList);
    		responseInfo.setMsg("添加成功");
    		responseInfo.setState(200);
    		
        	return responseInfo;
		} catch (MedicineServiceException e) {
			System.out.println("createController错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("createController服务方法错误");
        	

        	return responseInfo;
		}  catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("输入错误");
        	responseInfo.setState(500);

        	return responseInfo;
		}
    }
    
    /**
     * @param req
     * @return
     * 创建订单增加方法
     */
    
    @RequestMapping("create")
    @ResponseBody
    public ResponseInfo createController(HttpServletRequest req) {
    	
    		System.out.println("createController");
	    	String pharId = req.getParameter("pharId");
	    	System.out.println(pharId);
	    	//读出所有购物车的消息
	    	//生成总订单
	    	//生成订单条目
	    	//在库存中添加各类
		
    	try {
    		//验证库房是否存在
    		Pharmacy fakepharmacy = new Pharmacy();
    		fakepharmacy.setPharId(pharId);;
    		Pharmacy existphar = pharmacyService.findPharmacy(fakepharmacy);
    		if(existphar==null) {
    			throw new  NullPharmacyException();
    		}
    		//是否是空单
    		List<OrderCart> isCart = orderCartService.findAllCart();
    		System.out.println(isCart);
    		if(isCart.isEmpty()) {
    			throw new NullCartException();
    		}
    		
    		Emp user = (Emp) req.getSession().getAttribute("user");
    		System.out.println(user);
    		orderService.addOrder(pharId, user.getEmpName());
    		
    		
    		ResponseInfo responseInfo = new ResponseInfo();
    		responseInfo.setMsg("添加成功");
    		responseInfo.setState(200);
    		
        	return responseInfo;
		}catch (NullCartException e) {
			System.out.println("NullCartException错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("空单不能生成");
        	

        	return responseInfo;
		}catch (NullPharmacyException e) {
			System.out.println("NullPharmacyException错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("药房不存在");
        	

        	return responseInfo;
		}catch (MedicineServiceException e) {
			System.out.println("createController错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("createController服务方法错误");
        	

        	return responseInfo;
		}  catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("输入错误");
        	responseInfo.setState(500);

        	return responseInfo;
		}
    }
   

    
	
}
